package handlers

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"sanrenx.com/xyxf/app/services"
	"sanrenx.com/xyxf/pkg/app"
	"sanrenx.com/xyxf/pkg/e"
	"sanrenx.com/xyxf/pkg/utils"
)

// @Tags 文章
// @Summary 文章列表
// @Accept application/json
// @Produce  json
// @Param page query integer true "页码"
// @Param category query integer false "类型"
// @Param order query string false "排序 hot or new"
// @Success 200 {object} app.Response
// @Router  /app/v2/article [get]
func ArticleGetPageV2(c *gin.Context) {

	var (
		dataSrv services.ArticleSrv
		params  struct {
			PageNum  int    `form:"page" binding:"required,number"`
			Category int    `form:"category"`
			OrderBy  string `form:"order"`
		}
	)

	appG := app.Gin{C: c}
	//校验绑定参数
	if err := c.ShouldBind(&params); err != nil {
		app.MarkErrors(err)
		appG.Response(http.StatusBadRequest, e.InvalidParams, nil)
		return
	}

	pageNum := params.PageNum
	offset, size := utils.GetPage(pageNum)
	if res, err := dataSrv.GetPageArticlesByCategory(int64(offset), int64(size), []int{params.Category}, false, params.OrderBy); err == nil {
		//返回信息
		appG.Response(http.StatusOK, e.SUCCESS, map[string]interface{}{
			"page": pageNum,  //页码
			"size": len(res), //数量
			"list": res,      //文章列表
		})
		return
	}

	appG.Response(http.StatusInternalServerError, e.ErrorGetArticlesFail, nil)
}

// @Tags 文章
// @Summary 专辑收录文章列表
// @Accept application/json
// @Produce  json
// @Param page query integer true "页码"
// @Param id query integer true "专辑ID"
// @Success 200 {object} app.Response
// @Router  /app/v2/article/album [get]
func ArticleGetAlbum(c *gin.Context) {
	var (
		dataSrv services.ArticleSrv
		params  struct {
			PageNum int    `form:"page" binding:"required,number"`
			AlbumID uint64 `form:"id" binding:"required,number"`
		}
	)
	appG := app.Gin{C: c}
	//校验绑定参数
	if err := c.ShouldBind(&params); err != nil {
		app.MarkErrors(err)
		appG.Response(http.StatusBadRequest, e.InvalidParams, nil)
		return
	}

	pageNum := params.PageNum
	offset, size := utils.GetPage(pageNum)
	if res, err := dataSrv.GetPageArticlesByAlbum(params.AlbumID, offset, size); err == nil {
		//返回信息
		appG.Response(http.StatusOK, e.SUCCESS, map[string]interface{}{
			"page": pageNum,  //页码
			"size": len(res), //数量
			"list": res,      //文章列表
		})
		return
	}

	appG.Response(http.StatusInternalServerError, e.ErrorGetArticlesFail, nil)
}

// @Tags 文章
// @Summary 文章详情
// @Accept application/json
// @Produce json
// @Param id query integer true "文章ID"
// @Success 200 {object} app.Response
// @Router  /app/v2/article/details [get]
func ArticleGetDetailsV2(c *gin.Context) {

	var (
		articleSrv services.ArticleSrv
		userSrv    services.UserSrv
		params     struct {
			ArticleID uint64 `form:"id" binding:"required,number"`
		}
	)

	appG := app.Gin{C: c}
	//校验绑定参数
	if err := c.ShouldBind(&params); err != nil {
		app.MarkErrors(err)
		appG.Response(http.StatusBadRequest, e.InvalidParams, nil)
		return
	}

	//获取文章详情
	res, err := articleSrv.GetArticleById(params.ArticleID)
	if err != nil {
		appG.Response(http.StatusInternalServerError, e.ErrorGetArticleFail, nil)
		return
	}

	//用户登录
	if uid, err := appG.GetUID(); err == nil {
		//获取收藏状态
		if data, err := userSrv.GetFav(uid, params.ArticleID, "article"); err == nil {
			res.MyFav = data
		}
		//获取赞叹状态
		if data, err := userSrv.GetPraise(uid, params.ArticleID, "article"); err == nil {
			res.MyPraise = data
		}
	}

	//返回信息
	appG.Response(http.StatusOK, e.SUCCESS, res)
}
